package demo

import (
	"context"
	"fmt"
	"time"

	etcdclient "go.etcd.io/etcd/client/v3"
)

func init() {
	Register("EtcdWatch", func() Service {
		return new(EtcdWatch)
	})

	fmt.Println("EtcdWatch registered")
}

type EtcdWatch struct{}

func (ew *EtcdWatch) Exec() {
	client, err := etcdclient.New(etcdclient.Config{
		Endpoints:   []string{"http://localhost:2379"},
		DialTimeout: 5 * time.Second,
	})

	if err != nil {
		fmt.Printf("failed to etcd_watch: %v\n", err)
	}
	defer client.Close()

	rch := client.Watch(context.Background(), "q1mi")
	for wresp := range rch {
		for _, ev := range wresp.Events {
			fmt.Printf("Type: %s Key:%s Value:%s\n", ev.Type, ev.Kv.Key, ev.Kv.Value)
		}
	}
}
